﻿#include <cutil_math.h> // addded this to cli to make it work: -I "C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.2\C\common\inc"

extern "C"  
{
	__device__ const float particleMass = 0.0000000001f;
	__device__ const float galaxyMass = 0.000001f;
	__device__ const int GravParticles = 8;
	__global__ void kernel(float3* pos, float3* vel, int N)
	{
		int tid = threadIdx.x + blockIdx.x * blockDim.x;
		while (tid < N)
		{
			float3 tempVel = vel[tid];
			for (int i = 0; i < N / GravParticles; i++)
			{
				if (i == tid)
					continue;
				float3 vecTo = pos[i] - pos[tid];
				float acceleration = min(particleMass / dot(vecTo, vecTo), particleMass * 2048);
				tempVel += normalize(vecTo) * acceleration;
			}
			float3 vecToGlxy = -pos[tid];
			float accelerationGlxy = galaxyMass / dot(vecToGlxy, vecToGlxy);
			tempVel += normalize(vecToGlxy) * accelerationGlxy;
			if (dot(pos[tid], pos[tid]) > 16) {
				float3 toGlxy = normalize(vecToGlxy);
				tempVel = (toGlxy + cross(make_float3(0,0,1), vecToGlxy) * 0.1f) / 1024;
			}
			vel[tid] = tempVel;
			pos[tid] += tempVel;
			tid += blockDim.x * gridDim.x;
		}
	}

	__global__ void drawer(float3* pos, float2* arr, int N)
	{
		int tid = threadIdx.x + blockIdx.x * blockDim.x;
		while (tid < N)
		{
			arr[tid].x = pos[tid].x;
			arr[tid].y = pos[tid].y;
			tid += blockDim.x * gridDim.x;
		}
	}
}